---
title: Module Variables
group: Modules
sort: 8
contributors:
  - skipjack
  - sokra
  - ahmehri
  - tbroadley
  - byzyk
  - EugeneHlushko
  - wizardofhogwarts
  - anikethsaha
  - chenxsan
  - jamesgeorge007
  - snitin315
related:
  - title: CommonJS
    url: https://en.wikipedia.org/wiki/CommonJS
  - title: Asynchronous Module Definition
    url: https://en.wikipedia.org/wiki/Asynchronous_module_definition
---

This section covers all **variables** available in code compiled with webpack. Modules will have access to certain data from the compilation process through `module` and other variables.

## module.loaded (NodeJS)

This is `false` if the module is currently executing, and `true` if the sync execution has finished.

## module.hot (webpack-specific)

Indicates whether or not [Hot Module Replacement](/concepts/hot-module-replacement) is enabled and provides an interface to the process. See the [HMR API page](/api/hot-module-replacement) for details.

## module.id (CommonJS)

The ID of the current module.

```javascript
module.id === require.resolve('./file.js');
```

## module.exports (CommonJS)

Defines the value that will be returned when a consumer makes a `require` call to the module (defaults to a new object).

```javascript
module.exports = function doSomething() {
  // Do something...
};
```

W> This CANNOT be used in an asynchronous function.

## exports (CommonJS)

This variable is equal to the default value of `module.exports` (i.e. an object). If `module.exports` gets overwritten, `exports` will no longer be exported.

```javascript
exports.someValue = 42;
exports.anObject = {
  x: 123,
};
exports.aFunction = function doSomething() {
  // Do something
};
```

## global (NodeJS)

See [node.js global](https://nodejs.org/api/globals.html#globals_global).

For compatibility reasons webpack polyfills the `global` variable by default.

## \_\_dirname (NodeJS)

Depending on the configuration option `node.__dirname`:

- `false`: Not defined
- `mock`: equal to `'/'`
- `true`: [node.js \_\_dirname](https://nodejs.org/api/globals.html#globals_dirname)

If used inside an expression that is parsed by the Parser, the configuration option is treated as `true`.

## import.meta

The `import.meta` exposes context-specific metadata to a JavaScript module, such as the URL of the module. It is only available in [ESM](/guides/ecma-script-modules/).

Please note that webpack does not support direct access to `import.meta`. Instead, you should access its properties or use destructuring assignment. E.g.,

```js
// webpack will warn about this
Object.keys(import.meta);

// fine to use
console.log(import.meta.url);
const { url } = import.meta;
```

### import.meta.url

Returns the absolute `file:` URL of the module.

**src/index.js**

```javascript
console.log(import.meta.url); // output something like `file:///path/to/your/project/src/index.js`
```

### import.meta.webpack

Returns the webpack version.

**src/index.js**

```javascript
console.log(import.meta.webpack); // output `5` for webpack 5
```

### import.meta.webpackHot

Webpack specific. An alias for [`module.hot`](#modulehot-webpack-specific), however `import.meta.webpackHot` can be used in [strict ESM](/guides/ecma-script-modules/#flagging-modules-as-esm) while `module.hot` can't.

### import.meta.webpackContext

Returns the same value as `require.context` but only for `javascript/auto` and `javascript/esm`.

- Type:

  ```ts
  (
    request: string,
    options?: {
      recursive?: boolean;
      regExp?: RegExp;
      include?: RegExp;
      exclude?: RegExp;
      preload?: boolean | number;
      prefetch?: boolean | number;
      chunkName?: string;
      exports?: string | string[][];
      mode?: 'sync' | 'eager' | 'weak' | 'lazy' | 'lazy-once';
    }
  ) => webpack.Context;
  ```

- Available: 5.70.0+
- Example:

  ```js
  const contextRequire = import.meta.webpackContext('.', {
    recursive: false,
    regExp: /two/,
    mode: 'weak',
    exclude: /three/,
  });
  ```

## \_\_filename (NodeJS)

Depending on the configuration option `node.__filename`:

- `false`: Not defined
- `mock`: equal to `'/index.js'`
- `true`: [node.js \_\_filename](https://nodejs.org/api/globals.html#globals_filename)

If used inside an expression that is parsed by the Parser, the configuration option is treated as `true`.

## \_\_resourceQuery (webpack-specific)

The resource query of the current module. If the following `require` call was made, then the query string would be available in `file.js`.

```javascript
require('file.js?test');
```

**file.js**

```javascript
__resourceQuery === '?test';
```

## \_\_webpack_public_path\_\_ (webpack-specific)

Equals the configuration option's `output.publicPath`.

## \_\_webpack_require\_\_ (webpack-specific)

The raw require function. This expression isn't parsed by the Parser for dependencies.

## \_\_webpack_chunk_load\_\_ (webpack-specific)

The internal chunk loading function. Takes one argument:

- `chunkId` The id for the chunk to load.

Example to load chunks from alternate public path when one failed:

```js
const originalLoad = __webpack_chunk_load__;
const publicPaths = ['a', 'b', 'c'];
__webpack_chunk_load__ = async (id) => {
  let error;
  for (const path of publicPaths) {
    __webpack_public_path__ = path;
    try {
      return await originalLoad(id);
    } catch (e) {
      error = e;
    }
  }
  throw error;
};
import('./module-a').then((moduleA) => {
  // now webpack will use the custom __webpack_chunk_load__ to load chunk
});
```

## \_\_webpack_module\_\_ (webpack-specific)

<Badge text="5.68.0+" />

It provides access to the current `module`. `module` is not available in strict ESM.

## \_\_webpack_module\_\_.id (webpack-specific)

<Badge text="5.68.0+" />

It provides access to the ID of current `module` (`module.id`). `module` is not available in strict ESM.

## \_\_webpack_modules\_\_ (webpack-specific)

Access to the internal object of all modules.

## \_\_webpack_hash\_\_ (webpack-specific)

It provides access to the hash of the compilation.

## \_\_webpack_get_script_filename\_\_ (webpack-specific)

`function (chunkId)`

It provides filename of the chunk by its id.

It is assignable, which allows changing the filename used by the runtime. For example, it can be used to determine the final path when loading chunks.

```js
const oldFn = __webpack_get_script_filename__;

__webpack_get_script_filename__ = (chunkId) => {
  const filename = oldFn(chunkId);
  return filename + '.changed';
};
```

## \_\_non_webpack_require\_\_ (webpack-specific)

Generates a `require` function that is not parsed by webpack. Can be used to do cool stuff with a global require function if available.

## \_\_webpack_exports_info\_\_ (webpack-specific)

In modules, `__webpack_exports_info__` is available to allow exports introspection:

- `__webpack_exports_info__` is always `true`

- `__webpack_exports_info__.<exportName>.used` is `false` when the export is known to be unused, `true` otherwise

- `__webpack_exports_info__.<exportName>.useInfo` is

  - `false` when the export is known to be unused
  - `true` when the export is known to be used
  - `null` when the export usage could depend on runtime conditions
  - `undefined` when no info is available

- `__webpack_exports_info__.<exportName>.provideInfo` is

  - `false` when the export is known to be not provided
  - `true` when the export is known to be provided
  - `null` when the export provision could depend on runtime conditions
  - `undefined` when no info is available

- Accessing the info from nested exports is possible: i. e. `__webpack_exports_info__.<exportName>.<exportName>.<exportName>.used`

- Check whether exports can be mangled with `__webpack_exports_info__.<name>.canMangle`

## \_\_webpack_is_included\_\_ (webpack-specific)

<Badge text="5.16.0+" />

Test whether or not the given module is bundled by webpack.

```js
if (__webpack_is_included__('./module-a.js')) {
  // do something
}
```

## \_\_webpack_base_uri\_\_ (webpack-specific)

Change base URI at runtime.

- Type: `string`
- Available: 5.21.0+
- Example:

  ```js
  __webpack_base_uri__ = 'https://example.com';
  ```

## \_\_webpack_runtime_id\_\_

Access the [runtime](/blog/2020-10-10-webpack-5-release/#entry-point-runtime) id of current entry.

This is a webpack specific feature and it's available since webpack 5.25.0.

**src/index.js**

```js
console.log(__webpack_runtime_id__ === 'main');
```

## DEBUG (webpack-specific)

Equals the configuration option `debug`.
